package com.freesun.autoui.share;

import com.freesun.web.test.db.mysql.OperateMysql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by yangfei on 14-3-11.
 */

public class OperateDb {

    private static Logger log = LoggerFactory.getLogger(OperateDb.class);

    private static OperateMysql operateMysql;

    /*
 * @desc: 初始化建立连接
 */
    @Parameters({"SqlInfo", "index"})
    @BeforeTest
    public static void ConnectDb(String SqlInfo, String index) {
        OperateMysql.GetConnects(SqlInfo, Integer.valueOf(index));
    }


    /*
* @desc: 插入测试
*/
    public static void InsertTest(String idt) {

        Boolean bl = OperateMysql.InsertData(idt);
        Assert.assertTrue(bl);
    }


    /*
* @desc: 更新测试
*/
    public static void UpdateTest(String up) {

        int bl = OperateMysql.UpdateData(up);
        Assert.assertEquals(bl, 1);
    }


    /*
* @desc: 查询测试
*/
    public static void SelectTest(String st) {
        ResultSet rs = OperateMysql.SelectData(st);
        log.debug("编号" + "\t" + "姓名" + "\t" + "密码" + "\t" + "备注" + "\t" + "创建时间" + "\t" + "修改时间");
        try {
            while (rs.next()) {
                log.debug(rs.getInt(1) + "\t");
                log.debug(rs.getString(2) + "\t");
                log.debug(rs.getString(3) + "\t");
                log.debug(rs.getString(4) + "\t");
                log.debug(rs.getTimestamp(5) + "\t");
                log.debug(rs.getTimestamp(6) + "\t");
                log.debug("");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /*
 * @desc: 删除测试
 */
    public static void Delete(String dt){
        Boolean bl = OperateMysql.DeleteData(dt);
        Assert.assertTrue(bl);
    }


    /*
 * @desc: 关闭连接
 */
    @AfterTest
    public static void CloseConnectDb(){
        OperateMysql.ConnectClose();
    }

}
